package com.poetry.jianjia.slice;

import com.poetry.jianjia.ResourceTable;
import com.poetry.jianjia.net.Article;
import com.poetry.jianjia.presenter.HomePresenter;
import com.poetry.jianjia.provider.HomeArticleProvider;
import com.poetry.jianjia.util.LogUtils;
import com.poetry.jianjia.view.IHomeView;

import java.util.ArrayList;
import java.util.List;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;

public class MainAbilitySlice extends AbilitySlice implements IHomeView {

    private HomePresenter mHomePresenter;
    private ListContainer mListContainer;
    private HomeArticleProvider mHomeArticleProvider;
    List<Article.Data.Datas> mDatas;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_list);
        mDatas = new ArrayList<>();
        mHomeArticleProvider = new HomeArticleProvider(this, mDatas);
        mListContainer.setItemProvider(mHomeArticleProvider);
        mHomePresenter = new HomePresenter(this);
        mHomePresenter.getHomeArticle();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void setHomeArticle(Article article) {
        if (article == null || article.data == null || article.data.datas == null) {
            return;
        }
        mDatas.addAll(article.data.datas);
        mHomeArticleProvider.notifyDataChanged();
    }

    @Override
    public void onFailure(Throwable t) {
        LogUtils.info("yunfei", t.getMessage());
    }
}
